package lt.ntec.danblog.server.blog;

import java.util.List;
import javax.inject.Inject;
import lt.ntec.danblog.server.blog.model.Post;
import lt.ntec.danblog.server.blog.model.PostDAO;
import lt.ntec.danblog.server.core.CommentService;
import lt.ntec.danblog.server.core.Message;
import lt.ntec.danblog.server.core.MessageManager;
import lt.ntec.danblog.server.core.http.Flow;
import lt.ntec.danblog.server.core.http.RequestInfo;
import lt.ntec.danblog.server.core.model.Comment;
import lt.ntec.danblog.server.core.validator.ValidationException;
import lt.ntec.danblog.server.core.view.View;
import lt.ntec.danblog.shared.UrlPathConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Controller {

    private static final Logger LOG = LoggerFactory.getLogger(Controller.class);
    @Inject
    private View view;
    @Inject
    private CommentService commentService;
    @Inject
    private Service service;
    @Inject
    private MessageManager manager;
    @Inject
    private PostDAO postDAO;
    @Inject
    private RequestInfo requestInfo;
    @Inject
    private Flow flow;

    public void indexAction() {
        List<Post> posts = (List<Post>) postDAO.getLastPosts();
        view.setPart("posts", posts);
    }

    public void newAction() {
        flow.requireValidUser();

        if (requestInfo.isPost()) {

            try {
                String content = requestInfo.getPostParam("content");
                String tag = requestInfo.getPostParam("tags");
                String title = requestInfo.getPostParam("title");

                service.createPost(title, content, tag);

                manager.addMessage(Message.Type.NOTICE,
                                   "Post added");

                flow.redirect(UrlPathConsts.BLOG_POSTS);
            } catch (ValidationException e) {
                manager.addMessage(Message.Type.WARNING,
                                   "Post data is not valid. Please review it.");
                manager.addMessages(e.getMessages());
            } catch (BlogServiceException e) {
                manager.addMessage(Message.Type.ERROR,
                                   "Post could not be created. Contact administrator");
            }
        }
    }

    public void deleteAction() {
        Long postId = requestInfo.getLongUrlParam(0);
        service.deletePost(postId);
        manager.addMessage(Message.Type.NOTICE,
                           "Post has been deleted");
        flow.redirect("/blog/index/");
    }

    public void editAction() {
        Long postId = requestInfo.getLongUrlParam(0);
        Post post = postDAO.getById(postId);

        view.setPart("post", post);

        if (requestInfo.isPost()) {
            String title = requestInfo.getPostParam("title");
            String content = requestInfo.getPostParam("content");

            String tags = requestInfo.getPostParam("tags");
            try {
                service.updatePost(postId, title, content, tags);
                flow.redirect("/blog/view/" + postId);
            } catch (BlogServiceException ex) {
                LOG.error("Could not update blog post", ex);
            } catch (ValidationException ex) {
                manager.addMessage(Message.Type.WARNING,
                                   "Post data is not valid. Please review it.");
                manager.addMessages(ex.getMessages());
            }

        }
    }

    public void viewAction() {
        Long id = Long.parseLong(requestInfo.getUrlParam(0));
        Post post = postDAO.getById(id);
        view.setPart("post", post);
    }

    public void commentAction() {
        flow.requireValidUser();

        String action = requestInfo.getUrlParam(0);
        String postId = requestInfo.getUrlParam(1);


        if (action.equals("add") && requestInfo.isPost()) {
            String content = requestInfo.getPostParam("content");
            Long post = Long.valueOf(postId);
            try {
                commentService.addComment(post, content);
            } catch (ValidationException ex) {
                manager.addMessage(Message.Type.WARNING,
                                   "Post data is not valid. Please review it.");
            }
        }

        flow.redirect("/blog/view/" + postId);

    }

    public void editCommentAction() {
        Long commentId = requestInfo.getLongUrlParam(0);


        Comment comment = commentService.getComment(commentId);
        view.setPart("comment", comment);

        if (requestInfo.isPost()) {
            String content = requestInfo.getPostParam("content");
            try {
                commentService.updateComment(commentId, content);
            } catch (ValidationException ex) {
                manager.addMessage(Message.Type.WARNING,
                                   "Post data is not valid. Please review it.");
            }
            flow.redirect("/blog/view/" + comment.getParent().getId());
        }
    }

    public void deleteCommentAction() {
        Long commentId = requestInfo.getLongUrlParam(0);
        Comment comment = commentService.getComment(commentId);
        Long postId = comment.getParent().getId();
        commentService.removeComment(commentId);
        manager.addMessage(Message.Type.NOTICE,
                           "Comment has been deleted");
        flow.redirect("/blog/view/" + postId);
    }
}
